const Article = require("../model/article");
const formidable = require('formidable'); //处理含有文件上传的表单
const path = require('path');
const uuid = require('uuid'); //产生唯一字符串
const fs = require('fs');
/**
 * 文章中间件
 */
module.exports = {
  // 获取全部文章
  getHot: (req, res, next) => {
    Article.getHot()
      .then((res) => {
        req.hots = res;
        next();
      })
      .catch((err) => {
        next(err);
      });
  },
  /**
   * 获取最新文章
   */
  getList: (req, res, next) => {
    Article.getList()
      .then((res) => {
        req.article = res;
        next();
      })
      .catch((err) => {
        next(err);
      });
  },
  // 获取文章详情
  getArticleList: (req, res, next) => {
    let { id } = req.query;
    Article.getArticleList(id).then((res) => {
      req.articleList = res;
      next();
    }).catch(err => {
      next(err)
    });
  },
  // 获取分类目录下文章
  getListByCategoryId: (req, res, next) => {
    // 获取地址栏传递的id值
    let { category_id, hots } = req.query;
    Article.getListByCategoryId(category_id, hots)
      .then((res) => {
        let total = Math.ceil(res.length / 10);
        req.page = {
          total,
          p: res.length,
        };
        req.ListArticle = res
        next();
      })
      .catch((err) => {
        next(err);
      });
  },
  // 获取指定关键字的文章列表
  getListByKeyword: (req, res, next) => {
    // 获取地址栏传递的id值
    let { keyword } = req.query;
    Article.getListByKeyword(keyword)
      .then((res) => {
        req.keyword = res;
        next();
      })
      .catch((err) => {
        next(err);
      });
  },
  // 获取上一篇文章
  getPrev: (req, res, next) => {
    // 获取地址栏传递的id值
    let { id } = req.query;
    Article.getPrevArticle(id)
      .then((res) => {
        req.prevArticle = res;
        next();
      })
      .catch((err) => {
        next(err);
      });
  },
  // 获取下一篇文章
  getNext: (req, res, next) => {
    // 获取地址栏传递的id值
    let { id } = req.query;
    Article.getNextArticle(id)
      .then((res) => {
        req.nextArticle = res;
        next();
      })
      .catch((err) => {
        next(err);
      });
  },
  // 获取指定文章列表，实现分页功能
  getPage: (req, res, next) => {
    let { start, size } = res;
    let { category_id, hots } = req.query;
    Article.getPage(start, size, category_id, hots)
      .then((resolve) => {
        req.pageList = resolve;
        next();
      })
      .catch((err) => {
        next(err);
      });
  },
  // 设置热门推荐
  setHot: (req, res, next) => {
    let { id, hot } = req.query;
    Article.setHot(id, hot)
      .then((resolve) => {
        req.affectedRows = resolve;
        next();
      })
      .catch((err) => {
        next(err);
      });
  },
  // 文章添加
  articleAdd: (req, res, next) => {
    //处理含有文件上传的表单
    var form = new formidable.IncomingForm();
    //更改上传成功的临时文件的存放位置
    let fullpath = path.resolve(__dirname, '../static/upload');
    form.parse(req, function (err, fields, files) {
      if (!err) {
        // 判断是否有传递过来图片,有则对图片进行重命名
        if (files.description_image_path) {
          //获取文件扩展名
          let extname = path.extname(files.description_image_path.name);
          //完整文件名
          let fullname = uuid.v1() + extname;
          //将上传成功后的临时文件名改成正式文件名
          fs.renameSync(files.description_image_path.path, fullpath + '/' + fullname);
          //向fields对象中添加属性img
          fields.description_image_path = "/upload/" + fullname;

        }
        Article.articleAdd(fields)
          .then((resolve) => {
            req.insertId = resolve;
            next();
          })
          .catch((err) => {
            next(err);
          });
      }
    })

  },
  // 文章删除
  delArticle: (req, res, next) => {
    let { id } = req.body;
    Article.delArticle(id)
      .then((resolve) => {
        req.affectedRows = resolve;
        next();
      })
      .catch((err) => {
        next(err);
      });
  },
  // 编辑文章
  editArticle: (req, res, next) => {
    //处理含有文件上传的表单
    var form = new formidable.IncomingForm();
    //更改上传成功的临时文件的存放位置
    let fullpath = path.resolve(__dirname, '../static/upload');
    form.parse(req, function (err, fields, files) {
      if (!err) {
        // 判断是否有传递过来图片,有则对图片进行重命名
        if (files.description_image_path) {
          //获取文件扩展名
          let extname = path.extname(files.description_image_path.name);
          //完整文件名
          let fullname = uuid.v1() + extname;
          //将上传成功后的临时文件名改成正式文件名
          fs.renameSync(files.description_image_path.path, fullpath + '/' + fullname);
          //向fields对象中添加属性img
          fields.description_image_path = "/upload/" + fullname;

        }
        Article.editArticle(fields)
        .then((resolve) => {
          req.insertId = resolve;
          next();
        })
        .catch((err) => {
          next(err);
        })
      }
    })
    // Article.editArticle(article)
    //   .then((resolve) => {
    //     req.affectedRows = resolve;
    //     next();
    //   })
    //   .catch((err) => {
    //     next(err);
    //   });
  },
};
